package cn.school.workflow.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.dao.WorkPlanDao;
import cn.school.workflow.dao.WorkPlanPersonDao;
import cn.school.workflow.service.WorkPlanService;
import cn.school.workflow.vo.WorkPlanPersonVO;
import cn.school.workflow.vo.WorkPlanVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WorkPlanServiceImpl extends ServiceImpl<WorkPlanDao, WorkPlanVO> implements WorkPlanService {

    @Resource
    private WorkPlanPersonDao workPlanPersonDao;

    @Override
    public IPage<WorkPlanVO> findWorkPlanPageList(WorkPlanVO vo) {
        LambdaQueryWrapper<WorkPlanVO> wrapper = new LambdaQueryWrapper<>(vo);
        if(!StringUtils.isEmpty(vo.getPlanName())){
            wrapper.eq(WorkPlanVO::getPlanName,vo.getPlanName());
        }
        if(!StringUtils.isEmpty(vo.getPlanDept())){
            wrapper.eq(WorkPlanVO::getPlanDept,vo.getPlanDept());
        }
        if(!StringUtils.isEmpty(vo.getPlanBeginTime())){
            wrapper.ge(WorkPlanVO::getPlanBeginTime,vo.getPlanBeginTime());
        }
        if(!StringUtils.isEmpty(vo.getPlanBeginTime())){
            wrapper.le(WorkPlanVO::getPlanBeginTime,vo.getPlanBeginTime());
        }

        wrapper.orderByDesc(WorkPlanVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateWorkPlan(WorkPlanVO vo) {
        String userName = SysUserUtils.getSysUser().getUsername();
        int num=0;
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreateBy(userName);
            vo.setCreateTime(new Date());
            num = this.baseMapper.insert(vo);
            //添加计划执行人
            for(WorkPlanPersonVO personVO:vo.getWorkPlanPersonList()){
                personVO.setPlanId(vo.getId());
                personVO.setCreateBy(userName);
                personVO.setCreateTime(new Date());
                workPlanPersonDao.insert(personVO);
            }
        }else{
            vo.setModifyBy(userName);
            vo.setModifyTime(new Date());
            num = this.baseMapper.updateById(vo);
            //查询出当前计划执行人
            LambdaQueryWrapper<WorkPlanPersonVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WorkPlanPersonVO::getPlanId,vo.getId());
            wrapper.select(WorkPlanPersonVO::getId);
            List<WorkPlanPersonVO> personList = workPlanPersonDao.selectList(wrapper);
            //map用来检查修改的数据中是否有存量ID
            Map<String,Object> map = new HashMap<>();
            for(WorkPlanPersonVO newPerVO:vo.getWorkPlanPersonList()){
                if(StringUtils.isEmpty(newPerVO.getId())){//没有ID则新增
                    newPerVO.setPlanId(vo.getId());
                    newPerVO.setCreateBy(userName);
                    newPerVO.setCreateTime(new Date());
                    workPlanPersonDao.insert(newPerVO);
                }else{
                    //有ID则修改
                    newPerVO.setModifyBy(userName);
                    newPerVO.setModifyTime(new Date());
                    workPlanPersonDao.updateById(newPerVO);
                    map.put(String.valueOf(newPerVO.getId()),newPerVO.getId());
                }
            }
            for (WorkPlanPersonVO hisPerVO:personList){
                //map中没有的ID则要删除
                if(!map.containsKey(hisPerVO.getId())){
                    workPlanPersonDao.deleteById(hisPerVO.getId());
                }

            }


        }
        return num;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWorkPlanById(Long id) {
        int num = this.baseMapper.deleteById(id);
        LambdaQueryWrapper<WorkPlanPersonVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkPlanPersonVO::getPlanId,id);
        workPlanPersonDao.delete(wrapper);

        return num;
    }

    @Override
    public IPage<WorkPlanPersonVO> findPersonPlanListByPlanId(WorkPlanPersonVO vo) {
        int count =  workPlanPersonDao.findPersonPlanListByPlanIdCount(vo);
        int startNum=(vo.getPageNum()-1)*vo.getPageSize();
        vo.setPageNum(startNum);
        List<WorkPlanPersonVO> enrollList =  workPlanPersonDao.findPersonPlanListByPlanId(vo);
        IPage<WorkPlanPersonVO> page = new Page<>();
        page.setTotal(count);
        page.setRecords(enrollList);
        return page;
    }

    @Override
    public int updateMySafePlan(WorkPlanPersonVO vo) {
        vo.setModifyBy(SysUserUtils.getSysUser().getUsername());
        vo.setModifyTime(new Date());
        return workPlanPersonDao.updateById(vo);
    }
}
